﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Antu.WebControls
{
    /// <summary>
    /// 可编辑的下拉框控件
    /// </summary>
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DropDownText runat=server></{0}:DropDownText>")]
    public class DropDownText :TextBox,IUserControl
    {
        private Hashtable _values;
        private DropDownList _DropDownList;
       
        /// <summary>
        /// 设置数据源队列
        /// </summary>
        public Hashtable Values
        {
            get { return _values; }
            set { _values = value; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DropDownText()
        {
            _values = new Hashtable();
            _DropDownList = new DropDownList();
        }

        /// <summary>
        /// 绘制函数
        /// </summary>
        /// <param name="writer">绘制器</param>
        protected override void Render(HtmlTextWriter writer)
        {
            int iWidth = Convert.ToInt32(base.Width.Value);
            if (iWidth == 0)
            {
                iWidth = 102;
                base.Width = Unit.Parse("102px");
            }

            int sWidth = iWidth + 24;
            int spanWidth = sWidth - 18;

            writer.Write("<div style='POSITION:relative'>");
            writer.Write("<span style='MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px'>");

            _DropDownList.Width = Unit.Parse(sWidth.ToString() + "px");
            _DropDownList.Style.Add("MARGIN-LEFT", "-" + spanWidth.ToString() + "px");
            _DropDownList.ID = base.ID + "_Select";
            _DropDownList.Attributes.Add("onchange", "this.parentNode.nextSibling.value=this.value");
            _DropDownList.Attributes.Add("onfocus", "" + this.getFocusScript() + "");

            if (_values.Count > 0)
            {
                foreach (string key in _values.Keys)
                {
                    ListItem item = new ListItem();

                    item.Value = _values[key].ToString();
                    item.Text = key;

                    _DropDownList.Items.Add(item);
                }
            }
            _DropDownList.RenderControl(writer);

            writer.Write("</span>");

            base.Style.Clear();
            base.Width = Unit.Parse(iWidth.ToString() + "px");
            base.Style.Add("left", "0px");
            base.Style.Add("POSITION", "absolute");
            base.Render(writer);

            writer.Write("</div>");
        }

        /// <summary>
        /// 获取聚焦的脚本
        /// </summary>
        /// <returns></returns>
        private string getFocusScript()
        {
            string strScript = " ";
            strScript += "var isExist = -2; ";
            strScript += "var obj = event.srcElement; ";
            strScript += "var str = this.parentNode.nextSibling.value; ";
            strScript += "var ary = obj.options; ";
            strScript += "for(var i=0;i<ary.length;i++){ ";
            strScript += " if(str == ary[i].text){ ";
            strScript += "  isExist = i; ";
            strScript += "  break; ";
            strScript += " } ";
            strScript += "} ";
            strScript += "if(isExist != -2){ ";
            strScript += " obj.selectedIndex = isExist; ";
            strScript += "} ";
            strScript += "else{ ";
            strScript += " obj.selectedIndex = -1; ";
            strScript += "} ";

            return strScript;
        }

        #region IUserControl 成员

        /// <summary>
        /// 获取下拉框的选择值
        /// </summary>
        string IUserControl.Value
        {
            get
            {
                return _DropDownList.SelectedValue;
            }
            set
            {
                _DropDownList.SelectedValue = value;
            }
        }

        /// <summary>
        /// 设置只读属性
        /// </summary>
        bool IUserControl.ReadOnly
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                _DropDownList.Enabled = value;
                base.Enabled = value;
            }
        }

        /// <summary>
        /// 获取编辑框的值
        /// </summary>
        string IUserControl.Text
        {
            get 
            {
                return base.Text;
            }
        }

        /// <summary>
        /// 设置控件的宽度
        /// </summary>
        public Unit DdlWidth
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        #endregion
    }
}
